import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:home_framework/app.dart';
import 'package:home_framework/dynamic_detail.dart';

import 'package:home_framework/login.dart';
import 'package:home_framework/not_found.dart';
import 'package:home_framework/routers/permission_denied.dart';
import 'package:home_framework/routers/permission_router.dart';
import 'package:home_framework/splash.dart';
import 'package:home_framework/transition.dart';

class RouterManager {
  static String splashPath = '/';
  static String loginPath = '/login';
  static String homePath = '/home';
  static String dynamicPath = '/dynamic';
  static String dynamicDetailPath = '$dynamicPath/:id';
  static String transitionPath = '/transition';
  static String permissionDeniedPath = '/403';

  static PermissionRouter router;

  static void initRouter({List<String> whiteList}) {
    if (router == null) {
      router = PermissionRouter();
      defineRoutes(whiteList: whiteList);
    }
  }

  static final routeTable = {
    loginPath: Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return LoginPage();
    }),
    dynamicDetailPath: Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return DynamicDetailPage(params['id'][0]);
    }),
    splashPath: Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return Splash();
    }),
    transitionPath: Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return TransitionPage();
    }),
    homePath: Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return AppHomePage();
    }),
    permissionDeniedPath: Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return PermissionDenied();
    }),
  };

  static void defineRoutes({List<String> whiteList}) {
    router.whiteList = whiteList;
    router.permissionDeniedPath = permissionDeniedPath;
    routeTable.forEach((path, handler) {
      router.define(path, handler: handler);
    });

    router.notFoundHandler = Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return NotFound();
    });
  }
}
